package ui.panels;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JPanel;
import org.apache.log4j.Logger;


import Data.ConfigurationParameters;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import ui.flowManager.expirimentProperties.ExperimentProperties;
import ui.panels.PanelTypes.PanelType;
import ui.panels.primary.adaptiveAgentProperties.AdaptiveAgentProperties;
import ui.panels.primary.adaptiveAgents.AdaptiveAgentsPanel;
import ui.panels.primary.playerProperties.PlayerPropertiesPanel_2;
import ui.panels.primary.players.PlayersPanel;

public class ThirdPanel extends Panel{
	private static 		ArrayList<String> distributionList = null;
	private static 		ArrayList<String> strategyList = null;
	private static 		ArrayList<String> teammateModelList = null;
	private static 		ArrayList<String> analyzerlList = null;
	
	
	private JPanel 		adaptiveContainer;
	private JPanel 		playerPropertiesContainer;
	private JPanel 		playersContainer;
	private	GroupLayout thisLayout;
	
	private JPanel		lastPanelCreated = null;
	private final int 	gameNumber = 1;
	
	//Logger log = Logger.getLogger(this.getClass());
	
	public ThirdPanel(HashMap<String, String> map)
	{
		super();
	}
	
	
	public ThirdPanel() {
		// TODO Auto-generated constructor stub
	}


	@Override
	public PanelType getPanelType() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void show(ExperimentProperties props) {

		//log.debug("started show in second panel - Number Of Players");
			
		try {
			thisLayout = new GroupLayout((JComponent)this);
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(722, 480));
			{
				adaptiveContainer = new JPanel();
			}
			{
				playersContainer = new JPanel();
			}
			{
				playerPropertiesContainer = new JPanel();
			}

			//call to turn on 
			TurnOnPanels(props);
			
			thisLayout.setVerticalGroup(thisLayout.createParallelGroup()
					.addComponent(adaptiveContainer, GroupLayout.Alignment.LEADING, 0, 480, Short.MAX_VALUE)
					.addComponent(playersContainer, GroupLayout.Alignment.LEADING, 0, 480, Short.MAX_VALUE)
					.addComponent(playerPropertiesContainer, GroupLayout.Alignment.LEADING, 0, 480, Short.MAX_VALUE));
				thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
					.addComponent(adaptiveContainer, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE)
					.addComponent(playersContainer, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE)
					.addComponent(playerPropertiesContainer, 0, 339, Short.MAX_VALUE));
				
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}


	public JPanel getAdaptiveContainer() {
		return adaptiveContainer;
	}


	public JPanel getPlayerPropertiesContainer() {
		return playerPropertiesContainer;
	}


	public JPanel getPlayersContainer() {
		return playersContainer;
	}
	
	/**
	 * 
	 * 
	 */
	public void  TurnOnPanels(ExperimentProperties props)
	{
		/* -------	ADD THE PANELS	-------*/
		//addToPanelAdaptiveAgentsPanel(new GamesPanel(), props);
		addToPanelAdaptiveAgentsPanel(new AdaptiveAgentsPanel(gameNumber), props);
		addToPanelPlayersPanel(new PlayersPanel(gameNumber), props);
		//addToPanelPlayerPropertiesPanel(new PlayerPropertiesPanel_2(), props);	
	}
	
	/**
	 * @param component - should be GamesPanel
	 * 
	 * Create the Left side of the panel - Where we see table of the games
	 */
	public void addToPanelAdaptiveAgentsPanel( Component component, ExperimentProperties props)
	{

		if (component instanceof AdaptiveAgentsPanel)
		{
			AdaptiveAgentsPanel adaptiveAgentsPanel = (AdaptiveAgentsPanel)component;
			//save the panel
			lastPanelCreated = adaptiveAgentsPanel;
			
			adaptiveContainer.add(component);
			adaptiveAgentsPanel.show(props);
			
			component.repaint();
			
		}
	}
	
	
	/**
	 * @param component - should be PlayersPanel
	 * 
	 * Create the Middle side of the panel - Where we see table of the Players
	 */
	public void addToPanelPlayersPanel( Component component, ExperimentProperties props)
	{

		if (component instanceof PlayersPanel)
		{
			PlayersPanel playersPanel = (PlayersPanel)component;
			//save the panel
			lastPanelCreated = playersPanel;
			
			playersContainer.removeAll();	
			playersPanel.show(props);
			playersContainer.add(component);
			
			component.repaint();			
		}
	}	
	
	/**
	 * @param component - should be PlayersPanel
	 * 
	 * Create the right side of the panel - Where we see the Properties of the Player
	 */
	public void addToPanelPlayerPropertiesPanel( Component component, ExperimentProperties props)
	{
		

		if (component instanceof PlayerPropertiesPanel_2)
		{
			playerPropertiesContainer.removeAll();
			PlayerPropertiesPanel_2 playerPropertiesPanel = (PlayerPropertiesPanel_2)component;
			//save the panel
			lastPanelCreated = playerPropertiesPanel;			
			playerPropertiesPanel.show(props);
			playerPropertiesContainer.add(component);
				
			component.repaint();
		}
		
		else if (component instanceof AdaptiveAgentProperties)
		{
			playerPropertiesContainer.removeAll();
			AdaptiveAgentProperties adaptiveAgentProperties = (AdaptiveAgentProperties) component;
			//save the panel
			lastPanelCreated = adaptiveAgentProperties;
			adaptiveAgentProperties.show(props);
			playerPropertiesContainer.add(component);
			
			component.repaint();
		}
	
	}

	/*public void emptyProperties()
	{
		if (playerPropertiesContainer != null)
			playerPropertiesContainer.removeAll();
			playerPropertiesContainer.repaint();
	}*/
	
	@Override
	public void setFromPropertiesConfig(HashMap<String, String> map) {
		//set the distributions to the their list
		distributionList = new ArrayList<String>();
		String initString=readFromProps(map.get(ConfigurationParameters.Distribution.DISTRIBUTION));
		String[] values=initString.split(",");	
		for(int i = 0; i < values.length; ++i)
		{
			distributionList.add(values[i]);
		}
		
		//set the strategies to the their list
		strategyList = new ArrayList<String>();
		initString=readFromProps(map.get(ConfigurationParameters.Strategy.STRATEGY));
		values=initString.split(",");	
		for(int i = 0; i < values.length; ++i)
		{
			strategyList.add(values[i]);
		}
		
		
		//set the team mate models to the their list
		teammateModelList = new ArrayList<String>();
		initString=readFromProps(map.get(ConfigurationParameters.TeammateModel.TEAMMATE_MODEL));
		values=initString.split(",");	
		for(int i = 0; i < values.length; ++i)
		{
			teammateModelList.add(values[i]);
		}
		
		
		//set the team mate models to the their list
		analyzerlList = new ArrayList<String>();
		initString=readFromProps(map.get(ConfigurationParameters.Analyzer.ANALYZER));
		values=initString.split(",");	
		for(int i = 0; i < values.length; ++i)
		
		{
			analyzerlList.add(values[i]);
		}
		
		updatePropertiesConfigInnerPanels();
	}


	private void updatePropertiesConfigInnerPanels()
	{
		//for the studies
		AdaptiveAgentProperties.setAnalyzerlList(analyzerlList);
		AdaptiveAgentProperties.setDistributionList(distributionList);
		AdaptiveAgentProperties.setStrategyList(strategyList);
		AdaptiveAgentProperties.setTeammateModelList(teammateModelList);
		
		//for the control
		PlayerPropertiesPanel_2.setAnalyzerlList(analyzerlList);
		PlayerPropertiesPanel_2.setDistributionList(distributionList);
		PlayerPropertiesPanel_2.setStrategyList(strategyList);
		PlayerPropertiesPanel_2.setTeammateModelList(teammateModelList);
	}
	
	
	/*** @return lastPanelCreated*/
	public JPanel getLastPanelCreated() {
		return lastPanelCreated;
	}	
	
}
